﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using AutoMapper;
using FikriKibar.Common.Helpers;
using FikriKibar.DataAccess.Abstract;
using FikriKibar.DataAccess.ContextBase;
using FikriKibar.DataAccess.RepositoryBase;
using FikriKibar.Entities.Concrete;

namespace FikriKibar.DataAccess
{
    public class EfContentCategoryDal : EfEntityRepositoryBase<ContentCategory, FikriKibarDbContext>, IContentCategoryDal
    {
        protected override ContentCategory AddEntity(ContentCategory entity, FikriKibarDbContext context)
        {
            return context.ContentCategories.Add(entity);
        }

        protected override ContentCategory UpdateEntity(ContentCategory entity, FikriKibarDbContext context)
        {
            var oldEntity = GetEntity(entity, context);
            var newEntity = Mapper.Map(entity, oldEntity);
            context.Entry(oldEntity).CurrentValues.SetValues(newEntity);
            return newEntity;
        }

        protected override List<ContentCategory> GetEntities(FikriKibarDbContext context)
        {
            return context.ContentCategories
                .Include(e=>e.ParentContentCategory)
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.ContentType)
                .ToList();
        }

        protected override List<ContentCategory> FilterEntities(FikriKibarDbContext context, Expression<Func<ContentCategory, bool>> where)
        {
            return context.ContentCategories
                .Include(e=>e.ParentContentCategory)
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.ContentType)
                .Where(where).ToList();
        }

        protected override List<ContentCategory> FilterEntities(FikriKibarDbContext context, Expression<Func<ContentCategory, bool>> where, SortHelper<ContentCategory> sortHelper)
        {
            var query = (IOrderedQueryable<ContentCategory>) context.ContentCategories
                .Include(e=>e.ParentContentCategory)
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.ContentType)
                .Where(where);
            query = sortHelper.GenerateOrderedQuery(query);
            return query.ToList();
        }


        protected override List<ContentCategory> FilterEntities(FikriKibarDbContext context, Expression<Func<ContentCategory, bool>> where, SortHelper<ContentCategory> sortHelper, int index, int size)
        {
            var query = (IOrderedQueryable<ContentCategory>) context.ContentCategories
                .Include(e=>e.ParentContentCategory)
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.ContentType)
                .Where(where);
            query = sortHelper.GenerateOrderedQuery(query);
            return query.Skip((index - 1) * size).Take(size).ToList();
        }

        protected override ContentCategory GetEntity(ContentCategory entity, FikriKibarDbContext context)
        {
            return context.ContentCategories
                .Include(e=>e.ParentContentCategory)
                .Include(e=>e.User_CreatedBy)
                .Include(e=>e.User_UpdatedBy)
                .Include(e=>e.ContentType)
                .FirstOrDefault(e => e.Id == entity.Id);
        }

        public ContentCategory GetById(int? contentCategoryId)
        {
            using (var context = new FikriKibarDbContext())
            {
                return context.ContentCategories
                    .Include(e=>e.User_CreatedBy)
                    .Include(e=>e.User_UpdatedBy)
                    .Include(e=>e.ContentType)
                    .Include(e=>e.ParentContentCategory)
                    .FirstOrDefault(e => e.Id == contentCategoryId);


            }
        }

        public List<ContentCategory> GetParentList(ContentCategory contentCategory, List<ContentCategory> list)
        {
            if (contentCategory.ParentContentCategory == null) return list;
            list.Add(contentCategory);
            list = GetParentList(contentCategory.ParentContentCategory, list);
            return list;
        }

    }
}
